from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers

from .models import HardwareClass, ServiceClass, Server, SrvAccount


class HardwareClassSerializer(CustomModelSerializer):
    """硬件类别序列化器"""
    class Meta:
        model = HardwareClass
        fields = "__all__"

class ServiceClassSerializer(CustomModelSerializer):
    """服务类别序列化器"""
    class Meta:
        model = ServiceClass
        fields = "__all__"

class ServerSerializer(CustomModelSerializer):
    """服务器列化器"""
    
    class Meta:
        model = Server
        fields = "__all__"

class PhyServerSerializer(CustomModelSerializer):
    """服务器列化器"""
    
    in_use = serializers.SerializerMethodField()

    def get_in_use(self, obj):
         # 如果 offline_no 为空，则 in_use 为 True，否则为 False
        return not bool(obj.form_offline)
    
    class Meta:
        model = Server
        fields = "__all__"

class PhyServerCreateUpdateSerializer(CustomModelSerializer):
    """服务器创建更新序列化器"""

    class Meta:
        model = Server
        fields = "__all__"

class PhyServerImportSerializer(CustomModelSerializer):
    """服务器导入序列化器"""

    is_vm = serializers.BooleanField(default=False, read_only=True, help_text='是否为虚拟机')

    def run_validation(self, data={}):
        if type(data) is dict:
            data['hw_class'] = HardwareClass.objects.get(title=data.get('hw_class')).id
            data['service_class'] = ServiceClass.objects.get(title=data.get('service_class')).id
        return super().run_validation(data)

    class Meta:
        model = Server
        fields = "__all__"

class PhyServerExportSerializer(CustomModelSerializer):
    """服务器导出序列化器"""
    
    hw_class = serializers.CharField(source="hw_class.title", default= None, read_only=True, help_text='硬件类别')
    service_class = serializers.CharField(source="service_class.title", default= None, read_only=True, help_text='服务类别')

    class Meta:
        model = Server
        fields = "__all__"

class VmServerSerializer(CustomModelSerializer):
    """VM序列化器"""

    in_use = serializers.SerializerMethodField()

    def get_in_use(self, obj):
         # 如果 offline_no 为空，则 in_use 为 True，否则为 False
        return not bool(obj.form_offline)
    
    class Meta:
        model = Server
        fields = "__all__"

class VmServerCreateUpdateSerializer(CustomModelSerializer):
    """VM创建更新序列化器"""

    is_vm = serializers.BooleanField(default=True, help_text='是否为虚拟机')

    class Meta:
        model = Server
        fields = "__all__"

class VmServerImportSerializer(CustomModelSerializer):
    """VM导入序列化器"""

    is_vm = serializers.BooleanField(default=True, help_text='是否为虚拟机')
    hw_class = serializers.CharField(default= None, read_only=True, help_text='硬件类别')

    def run_validation(self, data={}):
        if type(data) is dict:
            data['service_class'] = ServiceClass.objects.get(title=data.get('service_class')).id
        return super().run_validation(data)

    class Meta:
        model = Server
        fields = "__all__"

class VmServerExportSerializer(CustomModelSerializer):
    """VM导出序列化器"""

    service_class = serializers.CharField(source="service_class.title", default= None, read_only=True, help_text='服务类别')
    
    class Meta:
        model = Server
        fields = "__all__"

class SrvAccountSerializer(CustomModelSerializer):
    """服务器账号序列化器"""

    server_ip = serializers.CharField(source="server.ipaddress", default= None, help_text='IP地址')

    class Meta:
        model = SrvAccount
        fields = "__all__"

class SrvAccountExportSerializer(CustomModelSerializer):
    """服务器账号导出序列化器"""

    server = serializers.CharField(source="server.hostname", default= None, read_only=True, help_text='主机名')
    server_ip = serializers.CharField(source="server.ipaddress", default= None, read_only=True, help_text='IP地址')

    class Meta:
        model = SrvAccount
        fields = "__all__"

class SrvAccountImportSerializer(CustomModelSerializer):
    """服务器账号导入序列化器"""

    def run_validation(self, data={}):
        if type(data) is dict:
            data['server'] = Server.objects.get(hostname=data.get('server')).id
        return super().run_validation(data)

    class Meta:
        model = SrvAccount
        fields = "__all__"

class SrvAccountCreateUpdateSerializer(CustomModelSerializer):

    class Meta:
        model = SrvAccount
        fields = "__all__"